<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据展示</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        button {
            margin: 5px;
            padding: 10px;
            font-size: 16px;
        }
    </style>
</head>
<body>
    <div id="app">
        <h1>数据展示</h1>
        <div>
            <button @click="startServer" :disabled="isRunning">Start Server</button>
            <button @click="stopServer" :disabled="!isRunning">Stop Server</button>
            <button @click="pauseServer" :disabled="!isRunning || isPaused">Pause Server</button>
            <button @click="resumeServer" :disabled="!isRunning || !isPaused">Resume Server</button>
        </div>
        <h2>数据</h2>
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Temperature (°C)</th>
                    <th>Humidity (%)</th>
                    <th>Door Status</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in modbusData" :key="item.id">
                    <td>{{ item.id }}</td>
                    <td>{{ item.temperature.toFixed(2) }}</td>
                    <td>{{ item.humidity.toFixed(2) }}</td>
                    <td>{{ item.doorStatus ? 'Open' : 'Closed' }}</td>
                </tr>
            </tbody>
        </table>
    </div>

    <script>
        new Vue({
            el: '#app',
            data: {
                isRunning: false,
                isPaused: false,
                modbusData: [{id:1, temperature: 20.5, humidity: 43.6, doorStatus: false}],
                updateInterval: null
            },
            methods: {
                startServer() {
                    axios.post('/api/start')
                        .then(response => {
                            this.isRunning = true;
                            this.startDataPolling();
                        })
                        .catch(error => console.error('Error starting server:', error));
                },
                stopServer() {
                    axios.post('/api/stop')
                        .then(response => {
                            this.isRunning = false;
                            this.isPaused = false;
                            this.stopDataPolling();
                        })
                        .catch(error => console.error('Error stopping server:', error));
                },
                pauseServer() {
                    axios.post('/api/pause')
                        .then(response => {
                            this.isPaused = true;
                        })
                        .catch(error => console.error('Error pausing server:', error));
                },
                resumeServer() {
                    axios.post('/api/resume')
                        .then(response => {
                            this.isPaused = false;
                        })
                        .catch(error => console.error('Error resuming server:', error));
                },
                startDataPolling() {
                    this.updateData();
                    this.updateInterval = setInterval(this.updateData, 1000);
                },
                stopDataPolling() {
                    clearInterval(this.updateInterval);
                },
                updateData() {
                    axios.get('/api/data')
                        .then(response => {
                            this.modbusData = response.data;
                        })
                        .catch(error => console.error('Error fetching data:', error));
                }
            },
            mounted() {
                // Check server status on page load
                axios.get('/api/status')
                    .then(response => {
                        this.isRunning = response.data.isRunning;
                        this.isPaused = response.data.isPaused;
                        if (this.isRunning && !this.isPaused) {
                            this.startDataPolling();
                        }
                    })
                    .catch(error => console.error('Error fetching server status:', error));
            }
        });
    </script>
</body>
</html>